package cn.toutatis.basics

/**
 * 08-循环
 * */
class A08_Loop {

    /*迭代循环*/
    fun loop1(): Unit {
        val items = listOf("apple", "banana", "kiwifruit")
        for (item in items) {
            println(item)
        }
    }

    /*索引循环*/
    fun loop2(): Unit {
        val items = listOf("apple", "banana", "kiwifruit")
        for (index in items.indices) {
            println("item at $index is ${items[index]}")
        }
    }

    /*条件循环*/
    fun whileLoop(): Unit {
        val items = listOf("apple", "banana", "kiwifruit")
        var index = 0
        while (index < items.size) {
            println("item at $index is ${items[index]}")
            index++
        }
    }

    /*区间表达式*/
    fun rangeExpressions(): Unit {

        println("===============")

        /*可以用 .. 操作符创建区间 */
        for (i in 1..3){
            println(i)
        }

        println("===============")

        /*反向迭代可使用 downTo */
        for (i in 4 downTo 1){
            println(i)
        }

        println("===============")

        /*选择步进可以使用 step */
        for (i in 10 downTo 0 step 2){
            println(i)
        }

    }
}

fun main() {
    A08_Loop().rangeExpressions()
}